<?php
class AdminAction extends  CommonAction {
	function getModel(){
		return D("Admin");
	}	
	
	function showlist(){
		$action=isset($_REQUEST['action'])?trim($_REQUEST['action']):'list';
		
		switch ($action){
			case 'list':
				$m=$this->getModel();
				//获取所有分类
				$c_arr=array();
				
				if($_REQUEST['username']!=''){
					$c_arr[]="username like '%{$_REQUEST['username']}%'";
				}
				
				if(count($c_arr)>0){
					$c=implode(" and ",$c_arr);
				}else{
					$c="1";
				}
				
				$count=$m->where($c)->count();
				
				import("ORG.Util.Page");
				$page=new Page($count,20);
				
				$list=$m->where($c)->limit($page->firstRow.','.$page->listRows)->order("createtime desc")->select();
				
				
				//echo $m->getLastSql();
				$this->assign('list',$list);
				$this->assign('page',$page->show());
				
				$this->display("showlist");
				break;
			case 'add':
				if(!$this->isPost()){
					$this->display("edit");
				}else{
					$m=D("Admin");
					if($m->create()){
						$id=$m->add();
						if($id){
							$this->assign('jumpUrl',U("/Admin/showlist"));
							$this->success("添加成功");
						}else{
							$this->error($m->getDbError());
						}
					}else{
						$this->error($m->getError());
					}
				}
				break;
			case 'edit':
				if(!$this->isPost()){
					$m=$this->getModel();
					$data=$m->getById(intval($_REQUEST['Id']));
					$this->assign('data',$data);
					//print_r($data);
					$this->display('edit');
				}else{
					$m=D("Admin");
					

					
					if($m->create()){
						if($_POST['password']==''){
							unset($m->password);
						}						
						
						$row=$m->save();
						//echo $m->getLastSql();
						$this->assign('jumpUrl',U("/Admin/showlist"));
						$this->success("保存成功");
					}else{
						$this->error($m->getError());
					}
				}
				break;
			case 'remove':
				if(!$this->isPost()){
					$m=$this->getModel();
					$Idlist=trim($_REQUEST['Idlist']);
					$rows=$m->where("Id in ($Idlist)")->delete();
					$this->success("成功删除");
				}
				break;
		}
	}
}